package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStore;
import com.zb.entity.ZjStoreInboundMaterial;
import com.zb.entity.ZjStoreOutboundMaterial;
import com.zb.entity.ZjStorePosition;
import com.zb.extend.StoreInboundMaterialExtend;
import com.zb.mapper.ZjStoreInboundMaterialMapper;
import com.zb.service.ZjStoreInboundMaterialService;
import com.zb.service.ZjStorePositionService;
import com.zb.service.ZjStoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 入库物料表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-12
 */
@Service
public class ZjStoreInboundMaterialServiceImpl extends ServiceImpl<ZjStoreInboundMaterialMapper, ZjStoreInboundMaterial> implements ZjStoreInboundMaterialService {
    @Resource
    private ZjStoreInboundMaterialMapper storeInboundMaterialMapper;
    @Resource
    private ZjStoreService storeService;
    @Resource
    private ZjStorePositionService storePositionService;

    // 根据入库单ID查询入库物料
    @Override
    public List<ZjStoreInboundMaterial> getbyStoreInboundId(Long storeInboundId) {
        QueryWrapper<ZjStoreInboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_inbound_id", "type", "store_id", "store_position_id", "goods_id", "goods_spec_id", "price", "qty");
        wrapper.eq("store_inbound_id", storeInboundId);
        return this.list(wrapper);
    }

    // 根据入库ID查询
    @Override
    public List<StoreInboundMaterialExtend> getbyStoreInboundIds(List<Long> storeInboundIds) {
        if (CollectionUtil.isEmpty(storeInboundIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreInboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_inbound_id", "type", "store_id", "store_position_id", "goods_id", "goods_spec_id", "price", "qty");
        wrapper.in("store_inbound_id", storeInboundIds);
        List<ZjStoreInboundMaterial> materialList = this.list(wrapper);
        List<ZjStore> storeList = storeService.getStoreNameByIds(materialList.stream().map(ZjStoreInboundMaterial::getStoreId).distinct().toList());
        List<ZjStorePosition> positionList = storePositionService.getStorePositionNameByIds(materialList.stream().map(ZjStoreInboundMaterial::getStorePositionId).distinct().toList());
        return materialList.stream().map(material -> {
            StoreInboundMaterialExtend extend = new StoreInboundMaterialExtend();
            BeanUtil.copyProperties(material, extend);
            storeList.stream().filter(f -> Objects.equals(f.getId(), material.getStoreId())).map(ZjStore::getName).findFirst().ifPresent(extend::setStore);
            positionList.stream().filter(f -> Objects.equals(f.getId(), material.getStorePositionId())).map(ZjStorePosition::getName).findFirst().ifPresent(extend::setStorePosition);
            return extend;
        }).toList();
    }

    // 根据入库ID获取入库物料 & 即时库存
    @Override
    public List<StoreInboundMaterialExtend> getMaterialsStockbyStoreInboundId(Long storeInboundId) {
        List<StoreInboundMaterialExtend> extendList = storeInboundMaterialMapper.getMaterialsStockbyStoreInboundId(storeInboundId);
        List<ZjStorePosition> positionList = storePositionService.getStorePositionNameByIds(extendList.stream().map(ZjStoreInboundMaterial::getStorePositionId)
                .filter(storePositionId -> null != storePositionId && storePositionId > 0).distinct().toList());
        extendList.forEach(t -> {
            positionList.stream().filter(f -> Objects.equals(f.getId(), t.getStorePositionId())).map(ZjStorePosition::getName).findFirst().ifPresent(t::setStorePosition);
        });
        return extendList;
    }

    // 根据仓位ID集合分组统计预入库数量
    @Override
    public List<ZjStoreInboundMaterial> getInStoreQtyGroupByStorePositionIds(List<Long> storePositionIds) {
        if (CollectionUtil.isEmpty(storePositionIds)) {
            return new ArrayList<>();
        }
        return storeInboundMaterialMapper.getInStoreQtyGroupByStorePositionIds(storePositionIds);
    }

    // 根据商品ID分组统计预入库数量
    @Override
    public List<ZjStoreInboundMaterial> getInStoreQtyByStoreIdOrStorePositionIdAndGroupByGoodsId(Long storeId, Long storePositionId, List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(goodsIds) || CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return storeInboundMaterialMapper.getInStoreQtyByStoreIdOrStorePositionIdAndGroupByGoodsId(storeId, storePositionId, goodsIds, goodsSpecIds);
    }

    // 根据仓库ID分组统计预入库数量
    @Override
    public List<ZjStoreInboundMaterial> getInStoreQtyByStoreIdGroupByStoreId(List<Long> storeIds, List<Long> goodsIds, List<Long> goodsSpecIds) {
        if (CollectionUtil.isEmpty(storeIds) || CollectionUtil.isEmpty(goodsIds) || CollectionUtil.isEmpty(goodsSpecIds)) {
            return new ArrayList<>();
        }
        return storeInboundMaterialMapper.getInStoreQtyByStoreIdGroupByStoreId(storeIds, goodsIds, goodsSpecIds);
    }

    // 根据入库ID更新入库物料状态
    @Override
    public void updateStatusById(Long storeInboundId, Integer status) {
        storeInboundMaterialMapper.updateStatusById(storeInboundId, status);
    }

    // 根据删除的仓库仓位ID获取关联入库的仓位ID
    @Override
    public List<Long> getStorePositionIdsByDelStorePositionIds(List<Long> delStorePositionIds) {
        if (CollectionUtil.isEmpty(delStorePositionIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreInboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.last("limit 10")
                .select("store_position_id")
                .in("store_position_id", delStorePositionIds);
        return this.list(wrapper).stream().map(ZjStoreInboundMaterial::getStorePositionId).distinct().toList();
    }

    // 根据仓库ID获取关联入库物料的仓库ID
    @Override
    public List<Long> getByStoreIds(List<Long> storeIds) {
        if (CollectionUtil.isEmpty(storeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreInboundMaterial> wrapper = new QueryWrapper<>();
        wrapper.select("store_id")
                .in("store_id", storeIds)
                .last("limit 10");
        return this.list(wrapper).stream().map(ZjStoreInboundMaterial::getStoreId).distinct().toList();
    }


}
